/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ac.ui.controls;

import ac.settings.dataModel;
import ac.ui.custemItmes;
import ac.uitls.propertiesOperation;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;

/**
 *
 * @author ismail
 */
public class PCLGetdataFull extends PCLcomboBox implements dataModel {

    private String addtype;
    private boolean category;
 private boolean required;

  

    public boolean isRequired() {
        return required;
    }

    public void setRequired(boolean required) {
        this.required = required;
    }

    private String type;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public boolean getCategory() {
        return category;
    }

    public void setCategory(boolean category) {
        this.category = category;
    }

    public String getAddtype() {
        return addtype;
    }

    public void setAddtype(String addtype) {
        this.addtype = addtype;
    }

    public final Vector model = new Vector();

    propertiesOperation op = new propertiesOperation();

    final String myt = "<-" + op.getWord("controls.add") + "->";

    public PCLGetdataFull() {

        model.addElement(new Item("", ""));
        model.addElement(new Item(myt, myt));
        setModel(new DefaultComboBoxModel(model));

        addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {

                ac.ui.controls.Item itemCate = (ac.ui.controls.Item) getItemAt(getSelectedIndex());

                if (itemCate.getValue() == myt) {

                    addData();
                }

            }
        });

    }

    ac.lib.database.operations db = new ac.lib.database.operations();

    String lep = "";

    private void updatech(Vector model, String id) {
        lep += "--";
        ResultSet rsc = db.getDataWithOutCon(tableAddstree, "`id`,`title`", "type='" + getAddtype() + "' and pid='" + id + "'");
        try {
            while (rsc.next()) {

                model.addElement(new Item(rsc.getString("id"), lep + rsc.getString("title")));

                updatech(model, rsc.getString("id"));
            }
        } catch (SQLException ex) {
            Logger.getLogger(PCLTree.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
  public void redef() {
        String[] v = db.getRow(tableAddstree, "`id`", "def='1' and type='" + getAddtype() + "'");
        ac.ui.utils.comBoxSetSelectonByValue(this, v[0]);
    }
    public void updateValue(String id) {
      
        model.addElement(new Item("", ""));
        model.addElement(new Item(myt, myt));
        setModel(new DefaultComboBoxModel(model));

        ac.uitls.xml x = new ac.uitls.xml();
        Map m;
        m = x.getTreeAdds(getAddtype());

        if (getCategory()) {

            String[] v = db.getRow(tableAddstree, "`id`,`title`", "id='" + id + "'");

            model.addElement(new Item(v[0], v[1]));

            ac.ui.utils.comBoxSetSelectonByValue(this, v[0]);

        } else {
            String tabel = "" + m.get("table");
            String name = "" + m.get("fieldname");
            String[] v = db.getRow(tabel, "`id`,`" + name + "`", "id='" + id + "'");
            model.addElement(new Item(v[0], v[1]));
            ac.ui.utils.comBoxSetSelectonByValue(this, v[0]);
        }

    }

    public void updateData() {
        //   DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode(new Item("0", op.getWord("label.all")));

        Vector model = new Vector();

    
        model.addElement(new Item("", ""));
        model.addElement(new Item(myt, myt));
        db.setOpenCon();
        ResultSet rs = db.getDataWithOutCon(tableAddstree, "`id`,`title`", "type='" + getAddtype() + "' and pid='0'");

        try {
            while (rs.next()) {

                model.addElement(new Item(rs.getString("id"), rs.getString("title")));

                updatech(model, rs.getString("id"));

            }

            setModel(new DefaultComboBoxModel(model));

        } catch (SQLException ex) {
            Logger.getLogger(PCLcomboboxAdd.class.getName()).log(Level.SEVERE, null, ex);
        }

        db.setCloseCon();

    }

    private void addData() {
        ac.uitls.xml x = new ac.uitls.xml();
        Map m;
        m = x.getTreeAdds(getAddtype());

        String type = "" + m.get("type");

        String title = "" + m.get("title");

        if (getCategory()) {

            ac.GUI.controls.getCategoryf p = new ac.GUI.controls.getCategoryf(type);
            p.Type = type;
            p.parentFull = this;

    p.setTitle(op.getWord(title));

            p.setVisible(true);
        } else {
            String tabel = "" + m.get("table");
            ac.GUI.controls.getData1 p = new ac.GUI.controls.getData1(type, tabel);
            p.Type = type;
            p.parentFull = this;

             p.setTitle(op.getWord(title));


            p.setVisible(true);
        }

    }
    
    
    public  String getValue() {

        String returnData = "";
        try {
            ac.ui.controls.Item itemCate = (ac.ui.controls.Item) this.getSelectedItem();
            returnData = itemCate.getValue();
        } catch (Exception ex) {
        }

        return returnData;
    }

    public  String getText( ) {

        String returnData = "";
        try {
            ac.ui.controls.Item itemCate = (ac.ui.controls.Item) this.getSelectedItem();
            returnData = itemCate.getText();
        } catch (Exception ex) {
        }

        return returnData;
    }

    public  void setSelectonByValue(  String value) {

        for (int i = 0; i < this.getItemCount(); i++) {
            if (value != null) {
                ac.ui.controls.Item itemCate = (ac.ui.controls.Item) this.getItemAt(i);
///if (!value.equals("")){
                if (itemCate.getValue().equals(value)) {
                    this.setSelectedIndex(i);
                }
            }
            ///  }

        }

    }

    public void setfill( String table, String valueFiled, String TextFilde) {
        Vector model = new Vector();
        db.setOpenCon();
        ResultSet rs = db.getDataWithOutCon(table);
        try {
            while (rs.next()) {
                model.addElement(new Item(rs.getString(valueFiled), rs.getString(TextFilde)));
            }
        } catch (SQLException ex) {
            Logger.getLogger(custemItmes.class.getName()).log(Level.SEVERE, null, ex);
        }
        this.setModel(new DefaultComboBoxModel(model));
        db.setCloseCon();
    }

    public void setfill( String[] data) {
        Vector model = new Vector();
        for (String d : data) {
            model.addElement(new Item(d, d));
        }
        this.setModel(new DefaultComboBoxModel(model));

    }

    public void setfill( String[] value, String[] text) {
        Vector model = new Vector();
        for (int i = 0; i < text.length; i++) {
            model.addElement(new Item(value[i], text[i]));
        }
        this.setModel(new DefaultComboBoxModel(model));
    }

}
